﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace The_LarX
{
    public class LarWebPage
    {
        private char Separador_Espaco = ' ';
        
        public List<string> ScriptLines;

        public WebPageInterpreter Interpreter;

        public static LarWebPage LoadPageFromWeb(string URL)
        {
#if WINDOWS
            WebClient client = new WebClient();
            Stream stream = client.OpenRead(URL);
            StreamReader reader = new StreamReader(stream);

            LarWebPage page = new LarWebPage();
            page.ScriptLines = new List<string>();
            while(!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                if (line != " " && line != "")
                    page.ScriptLines.Add(line);
            }

            return page;
#else
            return null;
#endif
        }

        private void CleanWebPage()
        {
            for (int i = 0; i < ScriptLines.Count; i++)
            {
                if (ScriptLines[i].Contains("\t"))
                {
                    ScriptLines[i] = ScriptLines[i].Replace("\t", "");
                }
            }
        }

        // Page values
        private bool PageIsLarWebPage;

        // Method values
        public WebPageMethod DrawMethod;
        private WebPageMethod InitMethod;

        public List<string> Variables;

        public void LoadWebPage()
        {
            CleanWebPage();

            Variables = new List<string>();
            PageIsLarWebPage = IsWebPage();
            DrawMethod = FindMethod("OnDraw");
            InitMethod = FindMethod("Init");

            DrawMethod.GetMethodCalls(this.ScriptLines);
        }
        public object CallMethodForVariable(string var)
        {
            for (int i = 0; i < DrawMethod.MethodCalls.Count; i++)
            {
                if (DrawMethod.MethodCalls[i].AssociatedVariable == var)
                { 
                    //return DrawMethod.MethodCalls[i].
                }
            }
            return null;
        }
        private bool IsWebPage()
        {
            for (int i = 0; i < ScriptLines.Count; i++)
            {
                if (ScriptLines[i].ToLower().Contains("[larw]"))
                    return true;
            }

            return false;
        }
        private WebPageMethod FindMethod(string MethodName)
        {
            bool foundMethod = false;
            int start = 0;
            for (int i = 0; i < ScriptLines.Count; i++)
            {
                if (ScriptLines[i].ToLower().Contains(("method " + MethodName).ToLower()))
                {
                    foundMethod = true;
                    start = i + 1;
                    break;
                }
            }
            if (foundMethod)
            {
                for (int i = start; i < ScriptLines.Count; i++)
                {
                    if (ScriptLines[i].ToLower().Contains("}"))
                    {
                        return new WebPageMethod(start, i, MethodName, this);
                    }
                }
            }

            return null;
        }
    }
}
